package com.lancoo.answer.view.adapter.wordSpell;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.entity.wordSpell.WordKeyDetailBean;

import java.util.List;

/**
 * Created by pengmi on 2022/1/19 15:16
 * Description:
 */
public class InputKeyBoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<WordKeyDetailBean> list;
    private OnInputClickCallBack onInputClickCallBack;

    public InputKeyBoardAdapter(List<WordKeyDetailBean> list) {
        this.list = list;
    }

    public void setOnInputClickCallBack(OnInputClickCallBack onInputClickCallBack) {
        this.onInputClickCallBack = onInputClickCallBack;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.ev_item_word_spell_input, parent, false);
        return new VH(inflate,onInputClickCallBack);
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VH vh= (VH) holder;
        vh.initData(list.get(position),position==list.size()-1);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class VH extends RecyclerView.ViewHolder implements View.OnClickListener{

        private final OnInputClickCallBack onInputClickCallBack;

        public VH(@NonNull View itemView,OnInputClickCallBack onInputClickCallBack) {
            super(itemView);
            this.onInputClickCallBack=onInputClickCallBack;
            itemView.setOnClickListener(this);
        }

        private void initData(WordKeyDetailBean bean,boolean isLast){
            TextView tv_answer = itemView.findViewById(R.id.tv_key);
            ImageView img_keyBgn=itemView.findViewById(R.id.img_key_bgn);
            View view_shadow=itemView.findViewById(R.id.view_shadow);
            tv_answer.setText(bean.getKeyStr());
            if(!isLast){
                if(bean.isSelected()||!bean.isEnable()){
                    img_keyBgn.setImageResource(R.mipmap.ev_ic_keybord_clicked );
                    view_shadow.setBackgroundResource(R.mipmap.ev_keybord_clicked_shadow);
                }else{
                    img_keyBgn.setImageResource(R.mipmap.ev_keybord_unclick);
                    view_shadow.setBackgroundResource(R.mipmap.ev_keybord_unclick_shadow);
                }
            }else{
                tv_answer.setText("确认");
                if(!bean.isEnable()){
                    img_keyBgn.setImageResource(R.mipmap.ev_ic_keybord_clicked );
                    view_shadow.setBackgroundResource(R.mipmap.ev_keybord_clicked_shadow );

                }else{
                    img_keyBgn.setImageResource(R.mipmap.ev_keybord_sure);
                    view_shadow.setBackgroundResource(R.mipmap.ev_keybord_sure_shadow);
                }
            }
        }

        @Override
        public void onClick(View v) {
            int position=getAdapterPosition();
            if(onInputClickCallBack!=null){
                onInputClickCallBack.onInputClick(position);
            }
        }
    }

    public static abstract class OnInputClickCallBack {

        public void onInputClick(int position){

        }
    }
}
